package com.dalongxin.work.page.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dalongxin.work.R;
import com.dalongxin.work.data.Contact;
import com.dalongxin.work.data.Department;
import com.dalongxin.work.view.CircularImage;
import com.dalongxin.work.view.expandRecyclerView.ExpandableAdapter;

import java.util.List;

public class ContactExpandAdapter extends ExpandableAdapter {

    private List<Department> departmentList;

    public ContactExpandAdapter(List<Department> departmentList) {
        this.departmentList = departmentList;
    }

    @Override
    protected int getGroupCount() {
        return departmentList == null ? 0 : departmentList.size();
    }

    @Override
    protected int getChildCount(int groupIndex) {
        if(departmentList != null && departmentList.size() > 0) {
            List<Contact> contactList = departmentList.get(groupIndex).getUserList();
            return contactList == null ? 0 : contactList.size();
        }
        return 0;
    }

    @Override
    protected RecyclerView.ViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact_group, parent, false);
        return new DepartmentViewHolder(v);
    }

    @Override
    protected void onBindGroupViewHolder(RecyclerView.ViewHolder holder, final int groupIndex) {
        if(departmentList != null) {
            Department item = departmentList.get(groupIndex);
            DepartmentViewHolder viewHolder = (DepartmentViewHolder)holder;
            if (item != null) {
                viewHolder.departmentName.setText(item.getDepartmentName());
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(isExpanded(groupIndex)) {
                            collapseGroup(groupIndex);
                        }else {
                            expandGroup(groupIndex);
                        }
                    }
                });
            }
        }
    }

    @Override
    protected RecyclerView.ViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false);
        return new ContactViewHolder(v);
    }

    @Override
    protected void onBindChildViewHolder(RecyclerView.ViewHolder holder, int groupIndex, int childIndex) {
        if(departmentList != null) {
            Department group = departmentList.get(groupIndex);
            List<Contact> contactList = group.getUserList();
            if(contactList != null) {
                Contact item = contactList.get(childIndex);
                ContactViewHolder viewHolder = (ContactViewHolder)holder;
                if (item != null) {
                    viewHolder.contactName.setText(item.getUserName());
                    viewHolder.contactPhone.setText(item.getPhone());
                }
            }
        }
    }

    public class DepartmentViewHolder extends RecyclerView.ViewHolder {
        private TextView departmentName;
        public DepartmentViewHolder(View itemView) {
            super(itemView);
            departmentName = itemView.findViewById(R.id.department_name);
        }
    }

    public class ContactViewHolder extends RecyclerView.ViewHolder {
        private CircularImage avatar;
        private TextView contactName;
        private TextView contactPhone;
        public ContactViewHolder(View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.contact_avatar);
            contactName = itemView.findViewById(R.id.contact_name);
            contactPhone = itemView.findViewById(R.id.contact_phone);
        }
    }
}
